home *** CD-ROM | disk | FTP | other *** search
- package com.commerceone.util.net;
-
- import java.io.Serializable;
- import java.net.URL;
-
- public class URI implements Serializable {
- String original;
- String scheme;
- String part;
- byte isGeneric;
- String authority;
- String path;
- String query;
-
- private void initGeneric() {
- this.isGeneric = 1;
- int index = this.part.indexOf(47, 2);
- if (index >= 2) {
- this.authority = this.part.substring(2, index);
- int start = index;
- index = this.part.indexOf(63, index);
- if (index >= 0) {
- this.path = this.part.substring(start, index);
- this.query = this.part.substring(index + 1);
- } else {
- this.path = this.part.substring(start);
- }
- }
-
- }
-
- private void setGeneric(String scheme, String authority, String path, String query) {
- this.isGeneric = 1;
- this.scheme = scheme;
- if (query == null) {
- this.part = "//" + authority + path;
- } else {
- this.part = "//" + authority + path + '?' + query;
- }
-
- this.authority = authority;
- this.path = path;
- this.query = query;
- this.original = scheme + ':' + this.part;
- }
-
- public String toString() {
- return this.scheme + ':' + this.part;
- }
-
- public URI(String uri) throws MalformedURIException {
- this.createURI(uri);
- }
-
- public URI(String scheme, String part) throws MalformedURIException {
- this.original = scheme + ":" + part;
- this.isGeneric = 0;
- this.scheme = scheme;
- this.part = part;
- if (part.length() == 0) {
- throw new MalformedURIException(this.original, "No scheme specific part has been specified.", 2);
- } else {
- this.authority = null;
- this.path = null;
- this.query = null;
- if (part.startsWith("//")) {
- this.isGeneric = -1;
- if (part.length() == 2) {
- throw new MalformedURIException(this.original, "No authority specified.", 3);
- }
- }
-
- }
- }
-
- public URI(URI base, String uri) throws MalformedURIException {
- this.original = uri;
- if (uri.charAt(0) == '/' && uri.charAt(1) == '/') {
- this.scheme = base.getDomainName();
- this.isGeneric = -1;
- this.part = uri;
- } else {
- int index = uri.indexOf(58);
- if (index < 0) {
- this.scheme = base.getDomainName();
- if (!base.isGeneric()) {
- throw new MalformedURIException(uri, "Cannot create a URI where base is non-generic and relative does not have an authority", 4);
- }
-
- this.isGeneric = 1;
- if (uri.charAt(0) == '/') {
- if (uri.charAt(1) == '/') {
- int nextSlash = uri.indexOf(47, 2);
- if (nextSlash < 0) {
- throw new MalformedURIException(uri, "No schem specific part", 2);
- }
-
- if (nextSlash < 3) {
- throw new MalformedURIException(uri, "No authority specified", 3);
- }
-
- if (nextSlash >= uri.length() - 1) {
- throw new MalformedURIException(uri, "No scheme specific part", 2);
- }
-
- this.authority = uri.substring(2, nextSlash);
- this.path = uri.substring(nextSlash);
- } else {
- this.path = uri;
- this.authority = base.getAuthority();
- }
- } else {
- this.authority = base.getAuthority();
- this.path = base.getPath();
- if (this.path.charAt(this.path.length() - 1) != '/') {
- int slash = this.path.lastIndexOf(47);
- if (slash > 0) {
- this.path = this.path.substring(0, slash + 1);
- } else {
- this.path = "/";
- }
- }
-
- this.path = this.path + uri;
- this.query = null;
- }
-
- char[] pathArray = new char[this.path.length()];
- this.path.getChars(0, this.path.length(), pathArray, 0);
- StringBuffer newPath = new StringBuffer();
- int i = 0;
-
- while(i < pathArray.length) {
- if ((i == 0 || pathArray[i - 1] == '/') && pathArray[i] == '.' && i < pathArray.length - 1 && pathArray[i + 1] == '/') {
- i += 2;
- } else {
- newPath.append(pathArray[i]);
- ++i;
- }
- }
-
- if (newPath.length() > 0 && newPath.charAt(newPath.length() - 1) == '.') {
- newPath.setLength(newPath.length() - 1);
- }
-
- this.path = newPath.toString();
- i = 0;
- int upDirStart = -1;
-
- while(!i && (upDirStart = this.path.indexOf("/../")) > 0) {
- int lastSlash = this.path.lastIndexOf(47, upDirStart - 1);
- if (lastSlash > -1) {
- this.path = this.path.substring(0, lastSlash + 1) + this.path.substring(upDirStart + 4);
- }
- }
-
- int quest = uri.indexOf(63);
- if (quest < 0) {
- this.part = "//" + this.authority + this.path;
- } else {
- this.query = uri.substring(quest + 1);
- this.part = "//" + this.authority + this.path + '?' + this.query;
- }
- } else {
- if (index == 0) {
- throw new MalformedURIException(uri, "No scheme specified.", 1);
- }
-
- this.scheme = uri.substring(0, index);
- this.part = uri.substring(index + 1);
- if (this.part.length() == 0) {
- throw new MalformedURIException(uri, "No scheme specific part has been specified.", 2);
- }
-
- this.authority = null;
- this.path = null;
- this.query = null;
- if (this.part.startsWith("//")) {
- this.isGeneric = -1;
- if (this.part.length() == 2) {
- throw new MalformedURIException(uri, "No authority specified.", 3);
- }
-
- int slashIndex = this.part.indexOf(47, 2);
- if (slashIndex < 2) {
- throw new MalformedURIException(uri, "No scheme specific slash has been specified.", 2);
- }
- }
- }
- }
-
- }
-
- public URI(URI originalU) {
- if (!originalU.isGeneric()) {
- this.scheme = originalU.getDomainName();
- this.part = originalU.getDomainIdentifier();
- this.original = this.scheme + ':' + this.part;
- this.isGeneric = 0;
- } else {
- this.setGeneric(originalU.getDomainName(), originalU.getAuthority(), originalU.getPath(), originalU.getQuery());
- }
-
- }
-
- public URI(String scheme, String authority, String path, String query) {
- if (path.charAt(0) != '/') {
- path = path + "/";
- }
-
- this.setGeneric(scheme, authority, path, query);
- }
-
- public URI(URL from) {
- this.scheme = from.getProtocol();
- this.isGeneric = 1;
- int port = from.getPort();
- if (port != -1) {
- this.authority = from.getHost() + ":" + port;
- } else {
- this.authority = from.getHost();
- }
-
- String file = from.getFile();
- if (file.charAt(0) != '/' && file.length() > 1) {
- file = "/" + file;
- }
-
- int queryIndex = file.indexOf(63);
- if (queryIndex >= 0) {
- this.query = file.substring(queryIndex + 1);
- file = file.substring(0, queryIndex);
- }
-
- String anchor = from.getRef();
- if (anchor != null) {
- file = file + "#" + anchor;
- }
-
- this.path = file;
- if (this.query == null) {
- this.part = "//" + this.authority + this.path;
- } else {
- this.part = "//" + this.authority + this.path + '?' + this.query;
- }
-
- this.original = this.scheme + ':' + this.part;
- }
-
- public String getQuery() {
- if (this.isGeneric < 0) {
- this.initGeneric();
- }
-
- return this.query;
- }
-
- public int hashCode() {
- return this.part.hashCode();
- }
-
- public String getDomainIdentifier() {
- return this.part;
- }
-
- public boolean isGeneric() {
- return this.isGeneric != 0;
- }
-
- public boolean equals(Object obj) {
- if (obj instanceof URI) {
- URI compareTo = (URI)obj;
- if (this.scheme.equals(compareTo.getDomainName()) && this.part.equals(compareTo.getDomainIdentifier())) {
- return true;
- }
- }
-
- return false;
- }
-
- public String getPath() {
- if (this.isGeneric < 0) {
- this.initGeneric();
- }
-
- return this.path;
- }
-
- public String getDomainName() {
- return this.scheme;
- }
-
- public String getOriginalURI() {
- return this.original;
- }
-
- public static boolean isAbsolute(String sysid) {
- return sysid.indexOf(58) > 0;
- }
-
- public String getAuthority() {
- if (this.isGeneric < 0) {
- this.initGeneric();
- }
-
- return this.authority;
- }
-
- protected void createURI(String uri) throws MalformedURIException {
- int index = uri.indexOf(58);
- if (index <= 0) {
- throw new MalformedURIException(uri, "No scheme has been specified.", 1);
- } else {
- String newScheme = uri.substring(0, index);
- String newPart = uri.substring(index + 1);
- if (newPart.length() == 0) {
- throw new MalformedURIException(uri, "No scheme specific part has been specified.", 2);
- } else {
- if (newPart.startsWith("//")) {
- if (newPart.length() == 2) {
- throw new MalformedURIException(uri, "No authority specified.", 3);
- }
-
- int slashIndex = newPart.indexOf(47, 2);
- if (slashIndex < 2) {
- throw new MalformedURIException(uri, "No scheme specific slash has been specified.", 2);
- }
-
- this.isGeneric = -1;
- } else {
- this.isGeneric = 0;
- }
-
- this.original = uri;
- this.scheme = newScheme;
- this.part = newPart;
- this.authority = null;
- this.path = null;
- this.query = null;
- }
- }
- }
- }
-